﻿
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Web.Script.Serialization;

namespace MNH.WebApps.Common.Helpers.JqGrid
{
  public class NullPropertiesConverter : JavaScriptConverter
  {
    public override IEnumerable<Type> SupportedTypes
    {
      get
      {
        return (IEnumerable<Type>) this.GetType().Assembly.GetTypes();
      }
    }

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
      throw new NotImplementedException();
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
      Dictionary<string, object> dictionary = new Dictionary<string, object>();
      foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
      {
        object obj1 = propertyInfo.GetValue(obj, BindingFlags.Public, (Binder) null, (object[]) null, (CultureInfo) null);
        if (obj1 != null)
          dictionary.Add(this.LowercaseFirst(propertyInfo.Name), obj1);
      }
      return (IDictionary<string, object>) dictionary;
    }

    private string LowercaseFirst(string s)
    {
      return (string) (object) char.ToLower(s[0]) + (object) s.Substring(1);
    }
  }
}
